package com.zhuiYun.project.api.shoppingcart.mapper;

import com.zhuiYun.project.api.shoppingcart.entity.ShoppingCartEntity;
import com.zhuiYun.project.common.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: ShoppingCartMapper
 * @descriptions: 购物车mapper
 * @Author: zq
 * @Date 2023/4/5 21:04
 */
public interface ShoppingCartMapper extends BaseMapper<ShoppingCartEntity> {

    /**
     * @description: 用户删除购物车商品方法
     * @author: zq
     * @date: 2023/4/7 9:04
     * @param: [id]
     * @return: int
    **/
    @Update("UPDATE shopping_cart SET del_flag = 1 WHERE id = #{id}")
    int delShoppingCartByUserId(Integer id);

    /**
     * @description: 用户删除选中购物车商品方法
     * @author: zq
     * @date: 2023/4/7 9:44
     * @param: [id]
     * @return: int
    **/
    @Update("<script>UPDATE shopping_cart SET del_flag = 1 WHERE " +
            "<foreach collection = 'ListId' item ='item' separator='or'>id = #{item}</foreach></script>")
    int delShoppingCart(@Param("ListId") List<Integer> ListId);

    /**
     * @description: 用户查询购物车信息
     * @author: zq
     * @date: 2023/4/7 18:27
     * @param: [userId]
     * @return: java.util.List<com.zhuiYun.project.api.shoppingcart.entity.ShoppingCartEntity>
    **/
    @Select("SELECT c.*,s.*,h.*,\n" +
            "(case when s1.seckill_price is null then c.favorable_price else s1.seckill_price end) AS favorable_price\n" +
            "FROM (SELECT * FROM shopping_cart WHERE user_id = #{userId} and del_flag = 0) s\n" +
            "LEFT JOIN  (SELECT id as cid,favorable_price,commodity_name,back,image_id,price,del_flag as cdel FROM commodity) c\n" +
            "ON s.commodity_id = c.cid\n" +
            "LEFT JOIN (SELECT id as hid,diy_name,diy_details,diy_price,diy_favorable_price,diy_image_id,del_flag as hdel FROM handwork_diy) h\n" +
            "ON s.diy_id = h.hid\n" +
            "LEFT JOIN (SELECT * FROM seckill WHERE del_flag = 0) s1\n" +
            "ON s1.comm_id = c.cid\n" +
            "WHERE c.cdel = 0 OR h.hdel = 0")
    List<Map> shoppingCartByUserId(Integer userId);

    /**
     * @description: 用户查询购物车商品数量
     * @author: zq
     * @date: 2023/4/7 20:20
     * @param: [userId]
     * @return: int
    **/
    @Select("SELECT count(s.id)\n" +
            "FROM (SELECT * FROM shopping_cart WHERE user_id = #{userId} and del_flag = 0) s\n" +
            "LEFT JOIN  (SELECT id as cid,favorable_price,commodity_name,back,image_id,price,del_flag as cdel FROM commodity) c\n" +
            "ON s.commodity_id = c.cid\n" +
            "LEFT JOIN (SELECT id as hid,diy_name,diy_details,diy_price,diy_favorable_price,diy_image_id,del_flag as hdel FROM handwork_diy) h\n" +
            "ON s.diy_id = h.hid\n" +
            "LEFT JOIN (SELECT * FROM seckill WHERE del_flag = 0) s1\n" +
            "ON s1.comm_id = c.cid\n" +
            "WHERE c.cdel = 0 OR h.hdel = 0")
    int shoppingCartCountByUserId(Integer userId);


    /**
    * @Author 赵笛
    * @Description TODO 根据商品id查询加入购物车的商品
    * @Date 2023/4/8 11:10
    * @Return com.zhuiYun.project.api.shoppingcart.entity.ShoppingCartEntity
    * @param commodityId
     * @param userId
    */
    @Select("select * from shopping_cart where commodity_id = #{commodityId} and user_id = #{userId} and del_flag = 0")
    ShoppingCartEntity getShoppingCartByCommodityId(@Param("commodityId")Integer commodityId,@Param("userId")Integer userId);

    /**
     * @Author 赵笛
     * @Description TODO 根据DIY id查询加入购物车的商品
     * @Date 2023/4/8 11:10
     * @Return com.zhuiYun.project.api.shoppingcart.entity.ShoppingCartEntity
     * @param diyId
     */
    @Select("select * from shopping_cart where diy_id = #{diyId} and user_id = #{userId} and del_flag = 0")
    ShoppingCartEntity getShoppingCartByDiyId(@Param("diyId")Integer diyId, @Param("userId")Integer userId);

    /**
    * @Author 赵笛
    * @Description TODO 修改当前购物车内商品的数量
    * @Date 2023/4/8 11:14
    * @Return int
    * @param number
    * @param commodityId
    * @param userId
    */
    @Update("UPDATE shopping_cart set number = #{number},update_time =NOW(),add_to_cart_date =NOW() WHERE commodity_id = #{commodityId} and user_id = #{userId}")
    int updateNumberByCommodityId(@Param("number")int number,@Param("commodityId")Integer commodityId,@Param("userId")Integer userId);

    /**
     * @description: 修改选中框状态为false
     * @author: zq
     * @date: 2023/4/9 18:46
     * @param: [listId]
     * @return: int
    **/
    @Update("<script>UPDATE shopping_cart SET flag = 'false' WHERE <foreach collection = 'ListId' item ='item' separator='or'>id = #{item}</foreach></script>")
    int wholeCheck(@Param("ListId") List<Integer> listId);

    /**
     * @description: 修改选中框状态为true
     * @author: zq
     * @date: 2023/4/9 19:23
     * @param: [listId]
     * @return: int
    **/
    @Update("<script>UPDATE shopping_cart SET flag = 'true' WHERE <foreach collection = 'ListId' item ='item' separator='or'>id = #{item}</foreach></script>")
    int wholeChecks(@Param("ListId") List<Integer> listId);

    /**
     * @description: 修改单选框状态为false
     * @author: zq
     * @date: 2023/4/10 15:37
     * @param: [id]
     * @return: int
    **/
    @Update("UPDATE shopping_cart SET flag = 'false' WHERE id = #{id}")
    int checkboxById(Integer id);

    /**
     * @description: 修改单选框状态为true
     * @author: zq
     * @date: 2023/4/10 15:37
     * @param: [id]
     * @return: int
     **/
    @Update("UPDATE shopping_cart SET flag = 'true' WHERE id = #{id}")
    int checkboxByIds(Integer id);

    /**
     * @Author 赵笛
     * @Description TODO 修改当前购物车内商品的数量
     * @Date 2023/4/8 11:14
     * @Return int
     * @param number
     * @param diyId
     * @param userId
     */
    @Update("UPDATE shopping_cart set number = #{number},update_time =NOW(),add_to_cart_date =NOW() WHERE diy_id = #{diyId} and user_id = #{userId}")
    int updateNumberByDiyId(@Param("number")int number, @Param("diyId")Integer diyId, @Param("userId")Integer userId);
}
